﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Entities;
using System.Web.Configuration;

namespace WXYW.BLL
{
    public class SpecialServiceService : RepositoryBase
    {
        public void AddSpecialServiceApplication(ApplicationProduct product)
        {
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            string applicationCode = string.Empty;
            string RMANO = product.ApplicationForm.ApplicationCode;
            IQueryable<SpecialServiceProduct> products = GetEntities<SpecialServiceProduct>().Where(s => s.ApplicationProduct.ApplicationProductID == product.ApplicationProductID);
            
            IQueryable<SpecialServiceForm> forms = GetEntities<SpecialServiceForm>().Where(s => s.ApplicationForm.ApplicationID == product.ApplicationForm.ApplicationID && s.Status.Equals("1"));
            if (forms.Count() > 0)
            {
                SpecialServiceForm form = forms.OrderByDescending(s => s.ApplicationDate).FirstOrDefault();
                SpecialServiceProduct applyProduct = new SpecialServiceProduct();
                product.ApplySpecialService = true;
                applyProduct.ApplicationProduct = product;
                form.SpecialServiceProduct.Add(applyProduct);
                UpdateEntity(form);
                applicationCode = form.ApplicationCode;
                RMANO = form.ApplicationForm.ApplicationCode;
            }
            else
            {
                SpecialServiceForm form = new SpecialServiceForm();
                form.Status = "1";
                form.ApplicationDate = DateTime.Now;
                form.ApplicationForm = product.ApplicationForm;
                form.ApplicationCode = RunningNumberService.GenerateSpecialServiceCode();
                SpecialServiceProduct applyProduct = new SpecialServiceProduct();
                applyProduct.ApplicationProduct = product;
                product.ApplySpecialService = true;
                form.SpecialServiceProduct.Add(applyProduct);
                InsertEntity(form);
                applicationCode = form.ApplicationCode;
            }

            // 发送邮件通知
            RoleService roleService = new RoleService();
            List<User> userList = roleService.GetUserHasPermission(84);
            foreach (User user in userList)
            {
                EmailService.SendEmail(user.UserEmail, WebConfigurationManager.AppSettings["TFSQNewAdminNoticeSubject"],
                    string.Format(WebConfigurationManager.AppSettings["TFSQNewAdminNoticeBody"], user.ContactPerson, RMANO));
            }              
        }

        public int GetCountOfApplication(string prefix)
        {
            return GetEntities<SpecialServiceForm>().Where(s => s.ApplicationCode.StartsWith(prefix)).Count();
        }

        public List<SpecialServiceForm> GetSpecialServiceApplicationListToDefine()
        {
            List<SpecialServiceForm> forms = new List<SpecialServiceForm>();
            IQueryable<SpecialServiceForm> entities = GetEntities<SpecialServiceForm>().Where(s => s.Status.Equals("1"));
            foreach (SpecialServiceForm entity in entities)
            {
                if (!entity.ApplicationFormReference.IsLoaded)
                {
                    entity.ApplicationFormReference.Load();
                }
                entity.Status = "特服申请-待处理";
                forms.Add(entity);
            }
            return forms;
        }

        public List<SpecialServiceForm> GetSpecialServiceApplicationListToFill()
        {
            List<SpecialServiceForm> forms = new List<SpecialServiceForm>();
            IQueryable<SpecialServiceForm> entities = GetEntities<SpecialServiceForm>().Where(s => s.Status.Equals("2"));
            foreach (SpecialServiceForm entity in entities)
            {
                if (!entity.ApplicationFormReference.IsLoaded)
                {
                    entity.ApplicationFormReference.Load();
                }
                entity.Status = "特服申请-待填";
                forms.Add(entity);
            }
            return forms;
        }

        public List<SpecialServiceForm> GetSpecialServiceApplicationListToFill(User currentUser)
        {
            List<SpecialServiceForm> forms = new List<SpecialServiceForm>();
            IQueryable<SpecialServiceForm> entities = GetEntities<SpecialServiceForm>().Where(s => s.Status.Equals("2") && s.User.UserID == currentUser.UserID);
            foreach (SpecialServiceForm entity in entities)
            {
                if (!entity.ApplicationFormReference.IsLoaded)
                {
                    entity.ApplicationFormReference.Load();
                }
                entity.Status = "特服申请-待填";
                forms.Add(entity);
            }
            return forms;
        }

        public List<SpecialServiceForm> GetSpecialServiceApplicationListByStatus(string status)
        {
            List<SpecialServiceForm> forms = new List<SpecialServiceForm>();
            IQueryable<SpecialServiceForm> entities = GetEntities<SpecialServiceForm>().Where(s => s.Status.Equals(status));
            foreach (SpecialServiceForm entity in entities)
            {
                if (!entity.ApplicationFormReference.IsLoaded)
                {
                    entity.ApplicationFormReference.Load();
                }
                switch (entity.Status)
                {
                    case "3":
                        entity.Status = "等待初审人签字";
                        break;
                    case "4":
                        entity.Status = "等待终审人签字";
                        break;
                    case "5":
                        entity.Status = "等待加签人签字";
                        break;
                }
                
                forms.Add(entity);
            }
            return forms;
        }

        public List<SpecialServiceForm> GetSpecialServiceApplicationListByStatus(string status, User currentUser)
        {
            List<SpecialServiceForm> forms = new List<SpecialServiceForm>();
            List<SpecialServiceForm> entities = null;
            switch (status)
            {
                case "3":
                    entities = GetEntities<SpecialServiceForm>().Where(s => s.Status.Equals(status) && s.User1.UserID == currentUser.UserID).ToList();
                    break;
                case "4":
                    entities = GetEntities<SpecialServiceForm>().Where(s => s.Status.Equals(status) && s.User2.UserID == currentUser.UserID).ToList();
                    break;
                case "5":
                    entities = GetEntities<SpecialServiceForm>().Where(s => s.Status.Equals(status) && s.User3.UserID == currentUser.UserID).ToList();
                    break;
            }

            foreach (SpecialServiceForm entity in entities)
            {
                if (!entity.ApplicationFormReference.IsLoaded)
                {
                    entity.ApplicationFormReference.Load();
                }
                switch (entity.Status)
                {
                    case "3":
                        entity.Status = "等待初审人签字";
                        break;
                    case "4":
                        entity.Status = "等待终审人签字";
                        break;
                    case "5":
                        entity.Status = "等待加签人签字";
                        break;
                }

                forms.Add(entity);
            }
            return forms;
        }

        public List<SpecialServiceForm> GetAllSpecialServiceApplicationList(string keyword)
        {
            //List<SpecialServiceForm> forms = new List<SpecialServiceForm>();
            List<SpecialServiceForm> forms = GetEntities<SpecialServiceForm>().ToList();

            foreach (SpecialServiceForm entity in forms)
            {
                if (!entity.ApplicationFormReference.IsLoaded)
                {
                    entity.ApplicationFormReference.Load();
                }
                switch (entity.Status)
                {
                    case "-1":
                        entity.Status = "特服申请-被驳回";
                        break;
                    case "1":
                        entity.Status = "特服申请-待定义";
                        break;
                    case "2":
                        entity.Status = "特服申请-待填";
                        break;
                    case "3":
                        entity.Status = "等待初审人签字";
                        break;
                    case "4":
                        entity.Status = "等待终审人签字";
                        break;
                    case "5":
                        entity.Status = "等待加签人签字";
                        break;
                    case "6":
                        entity.Status = "审批完成";
                        break;
                }

                //forms.Add(entity);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                return forms.Where(a => (a.ApplicationCode != null && a.ApplicationCode.Contains(keyword)) ||
                    (a.ApplicationForm.ApplicationCode != null && a.ApplicationForm.ApplicationCode.Contains(keyword)) ||
                    (a.ApplicationDate != null && a.ApplicationDate.Value.ToString("yyyy-MM-dd").Contains(keyword)) ||
                    (a.DefineDate != null && a.DefineDate.Value.ToString("yyyy-MM-dd").Contains(keyword)) ||
                    (a.Status != null && a.Status.Contains(keyword))).ToList();
            }
            return forms;
        }

        public SpecialServiceForm GetSpecialServiceByApplicationFormID(long applicationFormID)
        {
            return GetEntities<SpecialServiceForm>().Where(s => s.ApplicationForm.ApplicationID == applicationFormID).FirstOrDefault();
        }
    }
}
